﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SACM.Base;
using SACM.Common.Constantes;

namespace SACM.PageConsultorio
{
    public partial class DoctorRegistro : BasePage
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            toolbar.BtnCerrar.Attributes.Add("onclick", "return Regresar();");
            toolbar.BtnGuardar.Click += new EventHandler(BtnGuardar_Click);

            if (!Page.IsPostBack)
            {
                setDoctor();
            }
        }

        protected void BtnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                DoctorServiceReference.DoctorServiceClient servicio = new DoctorServiceReference.DoctorServiceClient();
                DoctorServiceReference.Doctor obj = new DoctorServiceReference.Doctor();
                obj.IdEmpresa = SessionEmpresa.IdEmpresa;
                obj.IdSucursal = SessionEmpresa.IdSucursal;
                obj.Nombre = txtNombre.Text;
                obj.ApellidoPaterno = txtApePat.Text;
                obj.ApellidoMaterno = txtApeMat.Text;
                obj.CedulaProfesional = txtCedula.Text;
                obj.RFC = txtRFC.Text;
                obj.DomicilioFiscal = txtDomFis.Text;
                obj.Telefono = txtTel.Text;
                obj.IpMod = Request.UserHostAddress;
                obj.IdUsuarioMod = SessionUsuario.IdUsuario;
                obj.IdDoctor = Common.Util.Convert.ToNInt(Request["IdDoctor"]);
                servicio.Save(obj);
                //Master.alertExito("Regresar();", true);
                Master.alertExito();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        #region Metodos
        private void setDoctor()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();
            //Parametros
            parametros.Add(CreaParametroConsulta("@psNombre", null, ConsultaServiceReference.Parameter.vEnumType.VarChar));
            parametros.Add(CreaParametroConsulta("@pnIdDoctor", Common.Util.Convert.ToNInt(Request["IdDoctor"]), ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdEmpresa", SessionEmpresa.IdEmpresa, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdSucursal", SessionEmpresa.IdSucursal, ConsultaServiceReference.Parameter.vEnumType.Int));
            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);
            //Ejecuta conulta
            DataSet ds = servicio.EjecutaConsultaConParametros(General.SACM + ".spObtenDoctores", prms);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                txtNombre.Text = dr[Columnas.Nombre].ToString();
                txtApePat.Text = dr[Columnas.ApellidoPaterno].ToString();
                txtApeMat.Text = dr[Columnas.ApellidoMaterno].ToString();
                txtCedula.Text = dr[Columnas.CedulaProfesional].ToString();
                txtRFC.Text = dr[Columnas.RFC].ToString();
                txtDomFis.Text = dr[Columnas.DomicilioFiscal].ToString();
                txtTel.Text = dr[Columnas.Telefono].ToString();
            }
        }

        #endregion
    }
}